package com.itheima.health.job;

import com.itheima.health.constant.RedisConstant;
import com.itheima.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 *
 * 定时任务清理垃圾图片服务
 * @author zhengjd
 * @date 2020/4/4 11:12
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 定时删除垃圾图片任务
     */
    public void clearImg(){
        //获取两个set集合的差集
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        for (String pic : sdiff) {
            //从七牛云中删除垃圾图片
            QiniuUtils.deleteFileFromQiniu(pic);

            //从Redis删除垃圾图片名称
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,pic);
            System.out.println("删除图片资源："+pic);
        }
    }
}
